# import multiprocessing
import multiprocessing

from flask import jsonify, render_template, request

from apps.settingApi.api.timerTask.apiList.jdYxk.wmsapi import jdYxkErpApiPrdt
from apps.settingApi.api.timerTask.task import add_task
from comm.appConfig import create_app
from flask_jwt_extended import JWTManager

from comm.openDbConfig import openDbBase
from comm.sqlalchemyConfig import db
from apps.model.initModel import baseAddData
from apps.model.createModel import modelUtilsMigration
from flask_compress import Compress
from flasgger import Swagger
from flask_cors import CORS

# from socketIo.main import start

app = create_app()
# 开启gzip数据压缩
compress = Compress(app)
# 配置跨域请求
CORS(app, supports_credentials=True)

swagger_config = {
    "swagger_version": "2.0",  # Swagger的版本
    "openapi": "3.0.2"

}
swagger = Swagger(app)

# 初始化JWT扩展
jwt = JWTManager(app)

#过期令牌
@jwt.expired_token_loader
def expired_token_callback(message,status):
    return jsonify({
        'msg': 'token过期',
        'error': '错误'
    }), 401
#无效令牌
@jwt.invalid_token_loader
def invalid_token_callback(message):
    return jsonify({
        'msg': '提供token无效请检查token',
        'error': '无效token'
    }), 401

# 上文管理初始化创建表
# 初始化创建表
with app.app_context():
    modelUtilsMigration()
    db.create_all()
    baseAddData(db)
dbproconfig = openDbBase()
# 启动flask方法
def startManage():
    print('启动flask')
    app.run(host="0.0.0.0", debug=True, port=dbproconfig['CHARTPORT'], threaded=True)

#
# # 启动socketio
# def startSocketio():
#     print('启动socketio')
#     start()

def starTIme():
    seconds = dbproconfig["TIME"]
    add_task(trigger='interval', seconds=seconds)
@app.route('/test')
def hello_test():
    args = request.args
    print(args)
    # jdYxkErpApiPrdt(db,None)
    # return {
    #     "or1":{
    #         "name":"a"
    #     },
    #     "or2":{
    #         "prd_no":"assd"
    #     },
    #     "or3":{
    #         "qty":3
    #     }
    # }
    if args.get('type_z',"1") == "1":

        return {
            "record":[
                    {
                        "qty":3
                    }
            ],
            "title":[
                {
                    "name": "dsdsa"
                }
            ]
        }
    else:
        return {
            "record": [
                {
                    "qty": 3
                },
                {
                    "qty": 100
                }
            ],
            "title": [
                {
                    "name": "dsdsa",
                    "rem":"aaa"
                }
            ],
            "footer": [
                {
                    "name": "dsdsa",
                    "rem": "aaa"
                }
            ]
        }
# 配置HTML5 History模式
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def hello_world(path):
    # wmsApiList.getToken()
    # u9cApiList.getToken()
    # wmsapiPullU9cPoMfCk(db,{"name":"进货退回"})
    return render_template('index.html')
if __name__ == '__main__':
#     # app.run(host="0.0.0.0",debug=True,port=5002,threaded=True)
#     # 创建两个进程
#     # process1 = multiprocessing.Process(target=startSocketio)
#     # process2 = multiprocessing.Process(target=startManage)
#     #
#     # # 启动进程
#     # process1.start()
#     # process2.start()
#     #
#     # # 等待所有进程完成
#     # process1.join()
#     # process2.join()
#     script_process = multiprocessing.Process(target=starTIme)
#     script_process.start()
    app.run(host="0.0.0.0",debug=True,port=dbproconfig['CHARTPORT'],threaded=True)